﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Accounting.Reports;

namespace School.Accounting
{
    public static class Discount
    {
        static SchoolEntities db = new SchoolEntities();

        public static List<DiscountReportObject> getAppliedDiscounts(List<long> expenses = null, DateTime? FromDate = null, DateTime? ToDate = null, long Campus = 0, long Certificate = 0, long Stage = 0, long Grade = 0, int Class = 0)
        {
            List<DiscountReportObject> discounts = new List<DiscountReportObject>();

            long currentYear = Structure.GetCurrentEductionalYear().First().Id;

            var v = from x in db.ACC_Voucher_Discount where x.ACC_Voucher.Current_Educational_Year == currentYear select x;

            if (FromDate != null)
                v = v.Where(x => x.ACC_Voucher.Date >= FromDate);

            if (ToDate != null)
                v = v.Where(x => x.ACC_Voucher.Date <= ToDate);

            if (Campus != 0)
                v = v.Where(x => x.ACC_Voucher.TE_Student.Campus_Id == Campus);

            if (Certificate != 0)
                v = v.Where(x => x.ACC_Voucher.TE_Student.Certificate_Id == Certificate);

            if (Stage != 0)
                v = v.Where(x => x.ACC_Voucher.TE_Student.Stage_Id == Stage);

            if (Grade != 0)
                v = v.Where(x => x.ACC_Voucher.TE_Student.Grade_ID == Grade);

            if (Class != 0)
                v = v.Where(x => x.ACC_Voucher.TE_Student.TE_Class_Student.Where(y => y.Student_Id == x.ACC_Voucher.Student_Id && y.EductionalYear_Id == currentYear).Select(z => z.Class_Id).First() == Class);

            if (expenses.Count() > 0)
            {
                v = v.Where(x => expenses.Contains(x.ACC_Voucher.Expense_Id));
            }

            foreach (var d in v)
            {
                var temp = new DiscountReportObject()
                {
                    DiscountName = d.ACC_Discount.Name,                    
                    Expense = d.ACC_Voucher.ACC_Expense.Name,
                    StudentName = d.ACC_Voucher.TE_Student.Ar_FirstName + " " + d.ACC_Voucher.TE_Student.Ar_SecondName + " " + d.ACC_Voucher.TE_Student.Ar_ThirdNmae + " " + d.ACC_Voucher.TE_Student.Ar_FourthName,
                    VoucherNumber = d.Voucher_Id,
                    VoucherDate = d.ACC_Voucher.Date
                };

                if (d.Discount_Type == 1)
                {
                    temp.DiscountType = "1";
                    temp.DiscountValue = d.Discount_Value;
                }
                else
                {
                    temp.DiscountType = "2";
                    temp.DiscountValue = d.Discount_Value;
                }

                temp.StudentNumber = d.ACC_Voucher.TE_Student.RegistrationNumber != null ? long.Parse(d.ACC_Voucher.TE_Student.RegistrationNumber.ToString()) : 0;

                discounts.Add(temp);
            }

            return discounts;
        }
    }
}
